package com.projectsexception.festivales.setup.xml;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public final class SAXHandlerUtil {

    private SAXHandlerUtil() {
        // Empty constructor
    }

    public static InputStream loadInputStreamSocket(String url) throws ParseadorException {
        InputStream in = null;
        int response = -1;
        try {
            URL urlObj = new URL(url);
            URLConnection conn = urlObj.openConnection();

            if (!(conn instanceof HttpURLConnection)) {
                throw new ParseadorException("No HTTP connection");
            }
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (MalformedURLException ex) {
            throw new ParseadorException("Error connecting");
        } catch (IOException e) {
            throw new ParseadorException("Error connecting");
        }
        return in;
    }

    public static void launchHandler(InputStream stream, ContentHandler handler) throws ParseadorException {
        if (stream != null) {
            InputSource is = new InputSource(stream);
            XMLReader xmlreader = null;
            try {
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser parser = factory.newSAXParser();
                xmlreader = parser.getXMLReader();
                xmlreader.setContentHandler(handler);
                xmlreader.parse(is);
            } catch (EndParsingSAXException e) {
                // Todo correcto
            } catch (SAXException e) {
                throw new ParseadorException(e);
            } catch (IOException e) {
                throw new ParseadorException(e);
            } catch (Exception e) {
                throw new ParseadorException(e);
            } catch (Error e) {
                throw new ParseadorException(e.getMessage());
            } finally {
                try {
                    stream.close();
                } catch (IOException e) {
                    // No hacer nada
                }
            }
        }
    }
    
    public static String cleanString(String string) {
        if (string != null) {
            String tmp = string.replaceAll("\n", " ");
            tmp = tmp.replaceAll("\t", " ");
            tmp = tmp.replaceAll("\\s\\s+", " ");
            return tmp.trim();
        } else {
            return null;
        }
    }
    
    public static String getElementName(String localName, String qName) {
        String name = localName;
        
        if (localName == null || localName.length() == 0) {
            int qual = qName.lastIndexOf(":");
            if (qual > 0) {
                name = qName.substring(qual + 1);
            } else {
                name = qName;
            }
        }
        return name;
    }

}
